package com.struts2.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Restrictions;

import com.struts2.model.Color;
import com.struts2.model.Product_item;
import com.struts2.model.Product_line;
import com.struts2.model.Size;

public class ProductUploadService2 {

	public Color colorModel(Color colorModel, int colorId){
		System.out.println("colorModel called");
		System.out.println(colorId);
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Color.class,
				"color");
		criteria.add(Restrictions.eq("color.id", colorId));
		colorModel = (Color) criteria.uniqueResult();
		session.getTransaction().commit();
		session.close();
		sF.close();
		System.out.println(colorModel);
		return colorModel;
	}
	
	public Size sizeModel(Size sizeModel, int sizeId){
		System.out.println("sizeModel called");
		System.out.println(sizeId);
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Size.class,
				"size");
		criteria.add(Restrictions.eq("size.id", sizeId));
		sizeModel = (Size) criteria.uniqueResult();
		session.getTransaction().commit();
		session.close();
		sF.close();
		System.out.println(sizeModel);
		return sizeModel;
	}
	
	public Product_line productLineModel(Product_line productLineModel, int productLineId){
		System.out.println("productLineModel called");
		System.out.println(productLineId);
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Product_line.class,
				"productLine");
		criteria.add(Restrictions.eq("productLine.id", productLineId));
		productLineModel = (Product_line) criteria.uniqueResult();
		session.getTransaction().commit();
		session.close();
		sF.close();
		System.out.println(productLineModel);
		return productLineModel;
	}
	
	public Product_item insert(Product_item productItem, Product_line productLineModel, Color colorModel, Size sizeModel, int quantity){
		System.out.println("insert called");
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		System.out.println(dateFormat.format(date));
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();
		productItem.setProduct_line(productLineModel);
		productItem.setColor(colorModel);
		productItem.setSize(sizeModel);
		productItem.setQuantity(quantity);
		productItem.setApproval(0);
		session.save(productItem);
		session.getTransaction().commit();
		session.close();
		sF.close();
		return productItem;
	}
	
}
